package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.ILeaveBizService;
import com.kk.xhr.admin.controller.request.ApplyQueryRequest;
import com.kk.xhr.admin.controller.request.LeaveAddRequest;
import com.kk.xhr.admin.controller.vo.LeaveVo;
import com.kk.xhr.common.pojo.ApplyStatusEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 休假管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/17
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "休假管理", tags = "休假管理")
@RequestMapping("/leave")
public class LeaveController {
    private final Executor adminControllerBizExecutor;
    private final ILeaveBizService leaveBizService;

    @ApiOperation(value = "查询我的休假申请", notes = "时间降序排序")
    @GetMapping
    public CompletionStage<ApiResult<PageVO<LeaveVo>>> queryMineLeave(@Valid ApplyQueryRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(leaveBizService.queryPage(userId, request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询审核休假申请", notes = "时间降序排序")
    @GetMapping("/checkLeave")
    public CompletionStage<ApiResult<PageVO<LeaveVo>>> queryCheckLeave(@Valid ApplyQueryRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(leaveBizService.queryPageCheck(userId, request)), adminControllerBizExecutor);
    }


    @ApiOperation(value = "休假申请")
    @PostMapping
    public CompletionStage<ApiResult<Void>> leave(@RequestBody @Valid LeaveAddRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            leaveBizService.leave(userId, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改请假状态")
    @PutMapping("/status/{id}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, ApplyStatusEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            leaveBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
